--[[---------------------------------------------------------
	INITIALIZATION
-----------------------------------------------------------]]

--[[-----------------------------------------------------]]--
--	Default config files paths
--[[-----------------------------------------------------]]--
local DEF_CFG_DYN_CL		= "gamemodes/<modname>/gamemode/config/client_dynamic.cfg"
local DEF_CFG_DYN_SRV		= "gamemodes/<modname>/gamemode/config/server_dynamic.cfg"
local DEF_CFG_STC_CL		= "gamemodes/<modname>/gamemode/config/client_static.cfg"
local DEF_CFG_STC_SRV		= "gamemodes/<modname>/gamemode/config/server_static.cfg"

--	Prepare static variable array
GM.Config					= {}

--[[---------------------------------------------------------
	GAMEMODE FUNCTIONS
-----------------------------------------------------------]]

--[[-----------------------------------------------------]]--
--	Occurs when a server con var is changed
--[[-----------------------------------------------------]]--
local function ServerConVarChanged( cvar, prev, n )

	--	Message
	DEBUG:Msg( "Server CVar " .. cvar .. " changed from " .. prev .. " to " .. n )

	--	Update list on server for future clients
	if GAMEMODE:IsBase() then
		GAMEMODE.ServerCVars[ cvar ] = { Prev = prev, Next = n }
	else
		GAMEMODE.BaseClass.ServerCVars[ cvar ] = { Prev = prev, Next = n }
	end
	
	--	Notify registered players client-side
	for k,v in pairs( player.GetAll() ) do
		if v.Registered then
			if GAMEMODE:IsBase() != nil then
				GAMEMODE:NotifyPlayerServerVarChanged( v, cvar, prev, n )
			else
				GAMEMODE.BaseClass:NotifyPlayerServerVarChanged( v, cvar, prev, n )
			end
		end
	end

end

--[[-----------------------------------------------------]]--
--	Loads a config file and creates the appropriate cvars
--[[-----------------------------------------------------]]--
function GM:LoadConfigFile( path, dynamic )

	--	Reference mod in path
	local p					= path:gsub( "<modname>", GAMEMODE_NAME )

	--	Read the file
	local contents			= file.Read( p, "GAME" )
	
	--	If not found or empty then read base mod config file
	if not contents or contents == "" then
		DEBUG:Msg( "Failed to load config file '" .. p .. "' - Trying base class..." )
		p					= p:gsub( GAMEMODE_NAME, GAMEMODE.BaseClass.Name:lower():gsub( "[.]", "" ) )
		contents			= file.Read( p, "GAME" )
	end
	
	--	Output an error if we still couldn't read it
	if not contents then
		DEBUG:ErrMsg( "Failed to load config file " .. p, "GM.LoadConfigFile" )
		return
	end
	
	--	Convert to table
	local config			= util.KeyValuesToTable( contents )
	if not config == nil then
		DEBUG:ErrMsg( "Failed to convert config file '" .. p .. "' into a table", "GM.LoadConfigFile" )
	end
	
	--	Create con vars / members for given key - values
	for k,v in pairs( config ) do
	
		--	Create the convar if it doesn't exist
		if tobool( dynamic ) and  !ConVarExists( k ) then
		
			--	Client con vars
			if CLIENT then
				DEBUG:Msg( "Creating client convar: " .. k .. " = " .. v )
				CreateClientConVar( k, v, false, false )
				
			--	Server con vars
			elseif SERVER then
				
				--	Save server cvar
				if self.ServerCVars == nil then
					self.ServerCVars = {}
				end
				self.ServerCVars[ k ] = { Prev = v, Next = v }
				
				--	Register it
				DEBUG:Msg( "Creating server convar: " .. k .. " = " .. v )
				cvars.AddChangeCallback( k, ServerConVarChanged )
				CreateConVar( k, v, FCVAR_NOTIFY )
				RunConsoleCommand( k, v )
			end
			
		--	Create member
		elseif not tobool( dynamic ) and self.Config[ k ] == nil then
			DEBUG:Msg( "Creating static var: " .. k .. " = " .. v )
			self.Config[ k ]	= v
		end
	
	end
	
	DEBUG:Msg( "Config file '" .. p .. "' successfully loaded" )

end

function GM:InitClientConfig()
	
	--	Client client con vars
	CreateClientConVar( "sc_footstep_mvt", 1, true, false )
	CreateClientConVar( "sc_custom_crosshair", 1, true, false )
	CreateClientConVar( "sc_dynamic_crosshair", 1, true, false )

end

--[[-----------------------------------------------------]]--
--	Loads all regular config files
--[[-----------------------------------------------------]]--
function GM:LoadConfigFiles()

	--	Load client config files
	if CLIENT then
	
		DEBUG:Msg( "Loading client config files" )
		self:InitClientConfig()
		--self:LoadConfigFile( DEF_CFG_DYN_CL, true )
		--self:LoadConfigFile( DEF_CFG_STC_CL, false )
	
	--	Load server config files
	elseif SERVER then
	
		DEBUG:Msg( "Loading server config files" )
		self:LoadConfigFile( DEF_CFG_DYN_SRV, true )
		self:LoadConfigFile( DEF_CFG_STC_SRV, false )
	
	end

end